package c.x.jy.admin.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import c.x.jy.common.annotation.IntoSet;
import c.x.jy.dao.dto.BusinessWalletEntityDto;
import c.x.jy.dao.entity.BusinessWallelogEntity;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.service.BusinessWallelogService;
import c.x.jy.service.service.BusinessWalletService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import c.x.jy.dao.entity.BusinessCashApplyEntity;
import c.x.jy.service.service.BusinessCashApplyService;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;



/**
 * 商家提现申请表
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-05-21 11:26:09
 */
@RestController
@RequestMapping("business/businesscashapply")
public class BusinessCashApplyController extends AbstractController {
    @Autowired
    private BusinessCashApplyService businessCashApplyService;
    @Autowired
    private BusinessWalletService businessWalletService;
    @Autowired
    private BusinessConfigService businessConfigService;
    @Autowired
    private BusinessWallelogService businessWallelogService;
    /**
     * 列表
     */
    @IntoSet(Includefields = {"businessName","checkUsername","cashUsername","applyUsername"})
    @RequestMapping("/list")
    @RequiresPermissions("business:businesscashapply:list")
    public R list(@RequestParam Map<String, Object> params){
     	if(getUser().getBusinessId()>0) {
    		params.put("businessId", getUser().getBusinessId());
    	}
        PageUtils page = businessCashApplyService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:businesscashapply:info")
    public R info(@PathVariable("id") Long id){
		BusinessCashApplyEntity businessCashApply = businessCashApplyService.getById(id);

        return R.put(businessCashApply);
    }
    /**
     * 商家提现申请提交
     */
    @Transactional
    @RequestMapping("/newapply")
    @RequiresPermissions("business:businesscashapply:save")
    public R newapply(@RequestBody BusinessCashApplyEntity businessCashApply){

        System.out.println(JSON.toJSONString(businessCashApply));
        SysUserEntity memberEntity = getUser();
        if(getUser().getBusinessId()==0) {
            return R.error("非商家账号，不能执行");
        }
        BusinessWalletEntityDto businessWallet=null;
        businessWallet = businessWalletService.getDtoById(memberEntity.getBusinessId());


        BigDecimal commissionBalance = businessWallet.getCommissionBalance();
        BigDecimal cashAmount = businessCashApply.getCashAmount();
        if(commissionBalance.compareTo(cashAmount)<0){
            return R.error("余额不足");
        }

        String fee = businessConfigService.getBusinessConfigValue(getUser().getBusinessId(),"BUSINESS_CASH_FEERATE","0");
        BigDecimal bigfee = new BigDecimal(fee);
        BigDecimal feeAmount = cashAmount.multiply(bigfee);
        BigDecimal actualAmount = cashAmount.subtract(feeAmount);

        //提现申请记录
        businessCashApply.setActualAmount(actualAmount);
        businessCashApply.setChargeFee(feeAmount);
        businessCashApply.setBusinessId(getUser().getBusinessId());
        businessCashApply.setApplyUserId(getUserId());
        businessCashApply.setStatus(1);
        businessCashApply.setCashStyle(1);
        businessCashApply.setDescription("商家提现申请");
        businessCashApplyService.save(businessCashApply);
        //钱包修改
        BigDecimal frozenC = businessWallet.getFrozenCommission();
        businessWallet.setCommissionBalance(commissionBalance.subtract(cashAmount));
        businessWallet.setFrozenCommission(frozenC.add(cashAmount));
        businessWalletService.updateById(businessWallet);
        //钱包操作日志
        BusinessWallelogEntity businessWallelogEntity = new BusinessWallelogEntity();
        businessWallelogEntity.setBusinessId(getUser().getBusinessId());
        businessWallelogEntity.setWalletItem(3);
        businessWallelogEntity.setAction(2);
        businessWallelogEntity.setAmount(cashAmount);
        businessWallelogEntity.setLinkOrder(businessCashApply.getId()+"");
        businessWallelogEntity.setModule("商家提现");
        businessWallelogEntity.setCreateId(getUserId());
        businessWallelogService.save(businessWallelogEntity);
        BusinessWallelogEntity businessWallelogEntity1 = new BusinessWallelogEntity();
        businessWallelogEntity1.setBusinessId(getUser().getBusinessId());
        businessWallelogEntity1.setWalletItem(4);
        businessWallelogEntity1.setAction(1);
        businessWallelogEntity1.setAmount(cashAmount);
        businessWallelogEntity1.setLinkOrder(businessCashApply.getId()+"");
        businessWallelogEntity1.setModule("商家提现");
        businessWallelogEntity1.setCreateId(getUserId());
        businessWallelogService.save(businessWallelogEntity1);

        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businesscashapply:save")
    public R save(@RequestBody BusinessCashApplyEntity businessCashApply){
        businessCashApplyService.save(businessCashApply);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businesscashapply:update")
    public R update(@RequestBody BusinessCashApplyEntity businessCashApply){
		businessCashApplyService.updateById(businessCashApply);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("business:businesscashapply:delete")
    public R delete(@PathVariable("id") Long id){
		businessCashApplyService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

